-
Notifications
You must be signed in to change notification settings - Fork 14
Add ability to parse egglog expressions into Python values #319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new type-safe mechanism for parsing egglog expressions into Python values. The key change replaces the deprecated .eval()
method with a new .value
property and introduces a comprehensive set of helper functions for deconstructing expressions.
Key changes:
- Replace all
.eval()
calls with.value
property access across tests and examples - Add new deconstruct.py module with type-safe expression parsing utilities
- Implement pattern matching support for egglog expressions with
__match_args__
Reviewed Changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
python/egglog/deconstruct.py | New module providing functions to safely extract callable functions, arguments, variable names, and literal values from expressions |
python/egglog/builtins.py | Replace deprecated .eval() methods with .value properties and add pattern matching support via __match_args__ |
python/egglog/runtime.py | Add metaclass infrastructure to support isinstance checks and pattern matching for runtime classes |
python/tests/test_*.py | Update all test files to use .value instead of .eval() for consistency |
python/egglog/declarations.py | Add match_args field to class declarations to support pattern matching |
docs/reference/python-integration.md | Update documentation to demonstrate new value extraction and pattern matching APIs |
CodSpeed WallTime Performance ReportMerging #319 will not alter performanceComparing Summary
|
CodSpeed Instrumentation Performance ReportMerging #319 will not alter performanceComparing Summary
|
This PR adds the ability to parse egglog expressions into Python values in a type safe manner.
See the documentation and tests for the new API.
This was done to try to make it easier to use egglog in situations like https://github.com/sdiehl/mlir-egglog where you want to extract a large expression, then traverse it in Python.
I went through many different iterations of this API, including making a few PRs to MyPy (python/mypy#19600 and python/mypy#19577) to try to find an API that was ergonomic and also as type safe as possible. Meaning that if you have a variable
x
of expression typeMath
and you want to see if is of functionfn
and get the args, then the return value offn
has to beMath
and the args should be properly typed as well.I would love if I could use Python builtin match statement but just use functions, but unfortunately it can only work with types not matching against other types of functions. At runtime, I could probably fiddle with things enough to make this pass, but statically it would still complain, which would defeat the purpose.
So instead, I settled on a function that you can call to return the args, which types them properly if it matches.
I also did add support for primitives inside match statements, and added support for custom classes to also support match statements with
__match_args__
. So if you have one "canonical" term for an expression, that it should get reduced to, you could match against that more easily.To do this, I had to make egglog
Expr
subclasses betype
s, which they weren't before. To keep most of our setup the same, I added a metaclass that made them types, which gets a bit hairy and possibly should be removed or refactored at a later date.I used @sdiehl's
mlir-egglog
project as an example to create this against. See the parsing in my fork of it to get a feel for how you could use this API.